package com.yisu.auction.api.core.service.impl;

import com.sun.xml.internal.rngom.parse.host.Base;
import com.yisu.auction.api.core.mapper.MobileRechargeMapper;
import com.yisu.auction.api.core.mapper.RechargeMapper;
import com.yisu.auction.api.core.service.BaseService;
import com.yisu.auction.api.core.service.IMobileRechargeService;
import com.yisu.auction.api.core.service.impl.func.MobileChargeUtil;
import com.yisu.auction.api.domain.request.BasePage;
import com.yisu.auction.api.domain.request.operatorBusiness.TelephoneChargeRequest;
import com.yisu.auction.api.domain.response.rechargePackge.RechargeListResponse;
import com.yisu.auction.api.domain.vo.mobileChargePackageVos.RechargeCallBackResponse;
import com.yisu.auction.api.domain.vo.mobileChargePackageVos.RechargeResponse;
import com.yisu.auction.ewf.entity.MobileRecharge;
import com.yisu.auction.ewf.entity.Recharge;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * author: 赵杰
 * Business Description：手机充值业务实现
 * createTime：2017/9/2
 */
@Service
public class MobileRechargeServiceImpl extends BaseService implements IMobileRechargeService {
    @Resource
    private MobileRechargeMapper mobileRechargeMapper;
    @Resource
    private RechargeMapper rechargeMapper;
    @Resource
    private MobileChargeUtil mobileChargeUtil;

    /**
     * author: 赵杰
     * Business Description：添加手机充值
     *
     * @param telephoneChargeRequest
     * @return
     */
    @Override
    public String addMobileRecharge(TelephoneChargeRequest telephoneChargeRequest) throws InterruptedException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmssSSS");//临时编号
        Thread.sleep(1000);
        //根据充值类型从枚举中获取订单头
        String orderId = MobileRecharge.RechargeTypeEnum.getEnumNameByCode(telephoneChargeRequest.getRechargeType()) + simpleDateFormat.format(new Date());
        Recharge insertModel = new Recharge();
        insertModel.setCreateDate(new Date());
        insertModel.setIsValid(true);
        insertModel.setType(Integer.valueOf(telephoneChargeRequest.getPayType()));
        insertModel.setMoney(BigDecimal.valueOf(telephoneChargeRequest.getAmount()));
        insertModel.setSysType(telephoneChargeRequest.getAppType());
        insertModel.setStatus(0);
        insertModel.setOrderNo(orderId);
        insertModel.setMemberId(Long.valueOf(telephoneChargeRequest.getMemberId()));
        rechargeMapper.insertSelective(insertModel);
        MobileRecharge mobileRecharge = new MobileRecharge();
        mobileRecharge.setCreateDate(new Date());
        mobileRecharge.setFace(Integer.valueOf(telephoneChargeRequest.getFace()));
        mobileRecharge.setMobile(telephoneChargeRequest.getServicenumber());
        mobileRecharge.setOrderNo(orderId);
        mobileRecharge.setStatus(1);
        mobileRecharge.setRechargeType(Integer.valueOf(telephoneChargeRequest.getRechargeType()));
        mobileRecharge.setDescription(telephoneChargeRequest.getDescription());
        mobileRecharge.setMemberId(telephoneChargeRequest.getMemberId());
        mobileRecharge.setMobileOperator(telephoneChargeRequest.getMobileOperator());
        if (telephoneChargeRequest.getRechargeType().equals("2")) {
            mobileRecharge.setPkgType(Integer.valueOf(telephoneChargeRequest.getPkgtype()));
        }
        mobileRechargeMapper.insertSelective(mobileRecharge);
        return orderId;
    }

    /**
     * author: 赵杰
     * Business Description：通过订单编号获取手机充值对象
     *
     * @param orderNo
     * @return
     */
    @Override
    public MobileRecharge getMobileRechargeByOrderNo(String orderNo) {
        return mobileRechargeMapper.getMobileRechargeByOrderId(orderNo);
    }

    /**
     * author: 赵杰
     * Business Description：回调
     *
     * @param rechargeCallBackResponse
     * @return
     */
    @Override
    public boolean notify(RechargeCallBackResponse rechargeCallBackResponse) {
        MobileRecharge mobileRecharge = mobileRechargeMapper.getMobileRechargeByOrderId(rechargeCallBackResponse.getAgentbillno());
        if (mobileRecharge == null)
            return false;
        MobileRecharge updateModel = new MobileRecharge();
        updateModel.setId(mobileRecharge.getId());
        updateModel.setStatus(3);
        updateModel.setThirdPartyOrderNo(rechargeCallBackResponse.getBillno());
        if (rechargeCallBackResponse.getPkgtype() != null && rechargeCallBackResponse.getPkgtype() != "") {
            updateModel.setPkgType(Integer.valueOf(rechargeCallBackResponse.getPkgtype()));
        }
        mobileRechargeMapper.updateByPrimaryKeySelective(updateModel);
        return true;
    }

    @Override
    public List<RechargeListResponse> getRechargeList(BasePage basePage) {
        List<RechargeListResponse> mobileRecharges = mobileRechargeMapper.selectByMemberId(basePage);
        for (RechargeListResponse rechargeListResponse : mobileRecharges) {
            rechargeListResponse.setImgUrl(rechargeListResponse.getMobileOperator(), rechargeListResponse.getRechargeType());
        }
        return mobileRecharges;
    }
}
